package cn.com.jtang.wedding.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.com.jtang.wedding.dao.UserDao;
import cn.com.jtang.wedding.model.User;

public class UserDaoImpl extends BaseDaoImpl implements UserDao{
	
	private static final String ID = "id";
	private static final String USERNAME = "userName";
	private static final String PASSWORD = "password";
	private static final String HUSBAND = "husband";
	private static final String WIFE = "wife";
	private static final String MOBILE = "mobile";
	private static final String EMAIL = "email";
	private static final String NOTES = "notes";
	private static final String REGISTTIME = "registTime";
	private static final String PROVINCE = "province";
	private static final String CITY = "city";
	private static final String PARENTID = "parentId";


	
	@Override
	public List insertModelToList(Object object) {
		User user = (User)object;
		List args = new ArrayList();
		args.add(0);
		args.add(user.getUserName());	
		args.add(user.getPassword());
		args.add(user.getHusband());
		args.add(user.getWife());
		args.add(user.getMobile());
		args.add(user.getEmail());
		args.add(user.getNotes());
		args.add(user.getRegistTime());
		args.add(user.getProvince());
		args.add(user.getCity());
		args.add(user.getParentId());
		return args;
	}

	@Override
	public Object resultMapper(ResultSet rs, int rowNum) throws SQLException {
		User user = new User();
		user.setId(rs.getLong(ID));
		user.setUserName(rs.getString(USERNAME));
		user.setPassword(rs.getString(PASSWORD));
		user.setHusband(rs.getString(HUSBAND));
		user.setWife(rs.getString(WIFE));
		user.setMobile(rs.getString(MOBILE));
		user.setEmail(rs.getString(EMAIL));
		user.setNotes(rs.getString(NOTES));
		user.setRegistTime(rs.getDate(REGISTTIME));
		user.setProvince(rs.getString(PROVINCE));
		user.setCity(rs.getString(CITY));
		user.setParentId(rs.getLong(PARENTID));
		return user;
	}

	@Override
	public Map updateModelToMap(Object object) {
		User user = (User)object;
		Map map = new HashMap();
		map.put(ID, user.getId());
		map.put(USERNAME, user.getUserName());
		map.put(PASSWORD, user.getPassword());
		map.put(HUSBAND, user.getHusband());
		map.put(WIFE, user.getWife());
		map.put(MOBILE, user.getMobile());
		map.put(EMAIL, user.getEmail());
		map.put(NOTES, user.getNotes());
		map.put(REGISTTIME, user.getRegistTime());
		map.put(PROVINCE, user.getProvince());
		map.put(CITY, user.getCity());
		map.put(PARENTID, user.getParentId());
		return map;
	}
	
	public String insert(User user){
		return super.insert(user);
	}
	
	public User queryById(Long id){
		return (User)super.queryById(id);
	}
	
	public int updateById(Long id, User user) {
		return super.updateById(id, user);
	}
	
	public int deleteById(Long id) {
		return super.deleteById(id);
	}
	
	public List queryAll(Map<String, Object> map, int start,int end, String... orderByStr){
		return super.queryAll(map, start, end, orderByStr);
	}
	
	public User queryByKey(String keyName, String keyValue){
		return (User)jdbcDao.queryByKey(this.tableName,keyName, keyValue, new ObjectMapper());
	}
}
